package com.sunbim.rsrv.service.impl;

import com.sunbim.rsrv.common.exception.ServiceException;
import com.sunbim.rsrv.mapper.UserMapper;
import com.sunbim.rsrv.pojo.Region;
import com.sunbim.rsrv.mapper.RegionMapper;
import com.sunbim.rsrv.pojo.User;
import com.sunbim.rsrv.service.RegionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 江苏省地区表 服务实现类
 * </p>
 *
 * @author yuhui
 * @since 2022-01-14
 */
@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements RegionService {

    @Autowired
    RegionMapper regionMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public List<Object> getCity() {
        return regionMapper.getCity();
    }

    @Override
    public List<Object> getZone(String city) {
        return regionMapper.getZone(city);
    }

    @Override
    public List<Object> getTown(String city, String zone) {
        return regionMapper.getTown(city, zone);
    }

    @Override
    public List<Object> getCityList() {
        return regionMapper.getCityList();
    }

    @Override
    public List<Object> getRegionList(Integer userId) {
        User user = userMapper.selectById(userId);
        Integer regionLevel = user.getRegionLevel();
        switch (regionLevel) {
            case 0:
                return getZone(user.getRegion());
            case 1:
                String[] split = user.getRegion().split(",");
                String city = split[0];
                String zone = split[1];
                return getTown(city, zone);
            default:
                throw new ServiceException("获取用户所属地区失败,该用户无区域数据");
        }
    }
}
